import hashlib

header_hex = ("01000000" +
    "81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +
    "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +
    "c7f5d74d" +
    "f2b9441a" +
     "42a14695")

header_bin = header_hex.decode('hex')
hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()

print hash.encode('hex_codec')

 # '1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000'

print hash[::-1].encode('hex_codec')
  #'00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'



print "last 2 Bytes", hash[-2:].encode('hex_codec') 
offMiddle=25000
for nonce in range(10000,99999):
    nonceCorrect=1    
    for rnsCount in range(0,55):    
        #print "rnsCount",rnsCount 
        allResidue=""   
        for modulus in [3,5,7]:
            #print "modulus", modulus
            residue = rnsCount % modulus
            allResidue=allResidue+"000"+str(residue)
        allResidue=allResidue+"0"            
        #print "allResidue", allResidue 
        header_hex =(allResidue+`nonce`)
        header_bin = header_hex.decode('hex')
        hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
        lastByte = int(hash[-2:].encode('hex_codec'),16)
        if (   (rnsCount<52 and lastByte>32000+offMiddle)
           or (rnsCount>=52 and lastByte<32000-offMiddle)) :            
            nonceCorrect=0
        
    if nonceCorrect:
        print "nonceCorrect",nonce        
    #print hash.encode('hex_codec')'''
print "Finished"  